﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Telerik.Web.Mvc;
using Century.Web.Security.Attributes;
using Century.Web.UI.MVC;
using WebMatrix.WebData;
using System.Web.Security;
using Century.Model;
using Century.Web.Security.Filters;
using Century.Business.CMS;

namespace Century.Admin.Controllers
{
    
    [Authorization(Group = "", Name = "Administration")]
    public class UserRoleManagementController : CenturyAdminBaseController
    {
        public UserRoleManagementController(ISiteService siteService) : base(siteService) { }
        //
        // GET: /AdminTools/UserRoleManagement/
        //UserBSO userBSO = new UserBSO();
        //RoleBSO roleBSO = new RoleBSO();
        CenturyDbContext db = new CenturyDbContext();

        public ActionResult Index()
        {
            
            return View();
        }

        [GridAction]
        public ActionResult GetUserList()
        {
             // MembershipUserCollection
                 
            var model = new GridModel
            {
                //Roles.GetAllRoles()
               
                Data = db.UserProfiles.ToList()//.Select(e => new { e.UserId, e.UserName, e.FirstName, e.LastName, e.IsAdministrator }).ToList()
            };
            return View(model);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult EditUser(int id)
        {
            var user = Membership.GetUser(id);
            //Century.Model.Common.User user = userBSO.Find(c => c.Id == id);
            TryUpdateModel(user);
            //userBSO.Update(user);
            Membership.UpdateUser(user);
            var model = new GridModel
            {
                Data = Membership.GetAllUsers()//userBSO.GetAll().Select(e => new { e.Id, e.UserName, e.FirstName, e.LastName, e.IsAdministrator })
            };
            return View(model);
        }

        [GridAction]
        public ActionResult GetRolesForUser(string userName)
        {
            //IEnumerable<Century.Models.Common.Role> roles = roleBSO.GetAll();
            //IEnumerable<Century.Models.Common.Role> userRoles = userBSO.GetRolesForUser(userId);
            //var confDetails = roles.Select(c =>
            //    new
            //    {
            //        c.Id,
            //        c.RoleName,
            //        c.DisplayName,
            //        UserHasThisRole = userRoles.Any(a => a.RoleName == c.RoleName)
            //    });
            //return View(new GridModel(confDetails));
            return View(new GridModel(Roles.GetRolesForUser("userName")));
        }

        public string ChangeUserRole(int userId, int roleId, bool userHasThisRole)
        {
            //Century.Models.Common.User user = userBSO.Find(c => c.Id == userId, "Roles");
            //Century.Models.Common.Role role = userBSO.FindRole(roleId);
            //if (userHasThisRole)
            //{
            //    user.Roles.Add(role);
            //}
            //else
            //{
            //    user.Roles.Remove(role);
            //}
            //userBSO.Update(user);
            return string.Empty;
        }

        [Authorization(Group = "", Name = "Administration")]
        public ActionResult ChangePasswordByAdmin(string userName)
        {

            return View(new KeyValuePair<string, string>(userName, string.Empty));
        }

        [Authorization(Group = "", Name = "Administration")]
        [HttpPost]
        public ActionResult ChangePasswordByAdmin(string UserName, string Value)
        {
            if (ModelState.IsValid)
            {

                // ChangePassword will throw an exception rather
                // than return false in certain failure scenarios.

               // new Century.Web.Security.CenturyMembershipProvider().ChangePasswordByAdmin(UserName, Value);



                return RedirectToAction("Index");


            }

            // If we got this far, something failed, redisplay form
            return View();
        }

        //[HttpPost]
        //public ActionResult AddUser(RegisterModel model)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        // Attempt to register the user
        //        MembershipCreateStatus createStatus;
        //        Membership.CreateUser(model.UserName, model.Password, model.Email, null, null, true, null, out createStatus);

        //        if (createStatus == MembershipCreateStatus.Success)
        //        {
        //            FormsAuthentication.SetAuthCookie(model.UserName, false /* createPersistentCookie */);
        //            return RedirectToAction("Index", "Home");
        //        }
        //        else
        //        {
        //            ModelState.AddModelError("", UIHelpers.ErrorCodeToString(createStatus));
        //        }
        //    }

        //    // If we got this far, something failed, redisplay form
        //    return View(model);
        //}

        
    }
}
